package com.wutong.eams.service;

import com.wutong.eams.dto.StudentManagerDTO;
import com.wutong.eams.entity.Student;
import com.wutong.eams.entity.StudentInfo;
import com.wutong.eams.mapper.StudentMapper;
import com.wutong.eams.other.ConditionBuilder;
import com.wutong.eams.other.Page;
import com.wutong.eams.utils.ObjectUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

@Slf4j
public class StudentManagerService implements IService<StudentMapper, Student> {

    private final StudentMapper studentMapper = new StudentMapper() {};

    @Override
    public StudentMapper getMapper() {
        return studentMapper;
    }

    public boolean addStudent(StudentManagerDTO.AddStudentDTO addStudentDTO) {
        return insert(addStudentDTO.getStudent()) > 0;
    }

    public boolean updateStudent(StudentManagerDTO.UpdateStudentDTO updateStudentDTO) {
        Student oldStudent = selectOne(
                new ConditionBuilder().where("student_id", ConditionBuilder.Operator.EQUAL,
                        updateStudentDTO.getStudent().getStudentId()));
        if (oldStudent == null) {
            return false;
        }
        ObjectUtil.copyProperties(updateStudentDTO.getStudent(), oldStudent, true);
        return update(oldStudent,
                new ConditionBuilder().where("student_id", ConditionBuilder.Operator.EQUAL,
                        updateStudentDTO.getStudent().getStudentId())) > 0;
    }

    public List<StudentInfo> getAllStudentInfos() {
        return getMapper().getAllStudentInfos();
    }

    public Page<StudentInfo> getAllStudentsWithPage(StudentManagerDTO.QueryAllStudentsWithPageDTO dto) {
        return getMapper().getAllStudentInfosByPage(dto.getPageNum(), dto.getPageSize());
    }

    public Page<StudentInfo> queryStudentsByCondition(StudentManagerDTO.QueryStudentsByConditionDTO dto) {
        return getMapper().getStudentInfosByCondition(
                dto.getClassId(),
                dto.getStudentNo(),
                dto.getKeyword(),
                dto.getPageNum(),
                dto.getPageSize()
        );
    }
}
